using UnityEngine;
using UnityEngine.UI;

public class ChargeBar : MonoBehaviour
{
    [Header("引用")]
    public Slider slider;
    public Image fillImage;

    [Header("参数")]
    public Color minColor = Color.green;  // 最小蓄力颜色
    public Color maxColor = Color.red;    // 最大蓄力颜色

    private DartController dartController;

    private void Start()
    {
        slider = GetComponent<Slider>();
        dartController = FindObjectOfType<DartController>();

        if (dartController == null)
        {
            Debug.LogError("未找到DartController组件！");
            return;
        }

        // 初始化滑块范围
        slider.minValue = 0f;
        slider.maxValue = dartController.maxChargePower;
    }

    private void Update()
    {
        if (dartController == null) return;

        // 更新蓄力条数值
        slider.value = dartController.CurrentCharge;

        // 根据蓄力值改变颜色
        float chargePercentage = dartController.CurrentCharge / dartController.maxChargePower;
        fillImage.color = Color.Lerp(minColor, maxColor, chargePercentage);
    }
}